<?php
namespace app\admin\controller;
use think\Db;
use think\request;
use think\Controller;

class Login  extends Controller
{
    public function index(){

    	// 已登录直接登录
    	if (session('admin_id')) {
            $this->redirect('admin/index/index');
        }
        return  $this->fetch();
    }

   
    public function dologin(){
    	 
    	
    	 if(request()->isPost()){
    	 	
    	 	$data['admin_name']= $_POST['admin_name'];
    	 	$data['admin_password']=md5_salt($_POST['admin_password']);
    	 	 if(!captcha_check($_POST['verify'])){//验证码判断
 					return json(['code'=>405,'msg'=>'验证码错误']);
			}
    	 	
    	 	$user=Db::name('tg_auth_user')->where($data)->find();
    	 	if($user){
				if($user['status'] != 1){
					
					$text='管理员【'.$user['admin_name'].'】登录';
	            	$status='登录成功';
	            	logc($text,$status);
            	
					$admin_title=Db::name('tg_auth_group')->where("id",$user['id'])->find();
					session('admin_title', $admin_title['title']);
					session('admin_id', $user['id']);
               		session('admin_name', $user['admin_name']);
               		return json(['code'=>200,'msg'=>$status]);
					//return $this->success("登陆成功","admin/index/index");
				}else{
					$text='<span style="color:red">用户【'.$data['admin_name'].'】用户已被禁用，登录失败</span>';
	            	$status='<span style="color:red">帐号有问题</span>';
	            	logc($text,$status);
	            	return json(['code'=>402,'msg'=>$status]);
				}
                
                
    	 	}else{
    	 		$text='<span style="color:red">用户【'.$data['admin_name'].'】登录失败，密码错误</span>';
            	$status='<span style="color:red">账户名或密码错误</span>';
            	logc($text,$status);
            	return json(['code'=>408,'msg'=>$status]);
			}
    	 	
    	 	
    	 }
    	
    	
   	}
  	
  	public function check_error(){
  		 return $this->error("没有权限","admin/index/index",2);
  	}
  	
  	/**
     * 退出登录
     */
    public function logout() {
        session('admin_id', null);
        session('admin_name', null);
        $this->success('退出成功', 'admin/login/index');
    } 
    
    
}
